﻿var ResponseSet = {}
//用户登陆 
function UserLogin(userName, nickName) {
    var RequestUrl = "http://cmd.com/LogIn?" + userName + "&" + "nickName";
    window.open(RequestUrl);
}
//用户退出 
function LogOut() {
    window.open("http://cmd.com/LogOut");
}

//获取在线用户 
function GetOnlineUser() {
    window.open("http://cmd.com/GetOnlineUser");
}

//显示左侧面板
function ShowToolBar() {
    window.open("http://cmd.com/ShowToolBar");
}

//隐藏程序面板
function HideWin() {
    window.open("http://cmd.com/HideWin");
}

//退出程序
function QuitApp() {
    window.open("http://cmd.com/QuitApp");
}

//启动PPT控制程序打开PPT文件
//fileUrl文件绝对路径
function OpenPpt(fileUrl) {
    window.open("http://cmd.com/OpenPpt?" + fileUrl);
}

//向指定用户发送消息
//消息类型
//接收账号
//消息内容
function SendMsg(Type, SendTo, SendMsg) {
    var ResqustContent = "<CMD Name=\"" + Type + "\" Param=\"" + SendMsg + "\"/>";
    window.open("http://cmd.com/SendMsg?" + SendTo + "&" + ResqustContent);
}

//openfire客户端消息弹出
function PopMsg(Message) {
    layer.alert(message)
    //window.open("http://cmd.com/PopMsg?" + Message);
}
//改版后获取到的打开网页教师消息回调此函数，通过time区分是否为离线消息，非离线消息需要自己打开不再交由客户端处理。
function WebOpen(addr, time) {
    // http://192.168.1.101:96/ReTest/Index?
    // ResourceID=5118&VoteID=199011221535&LessionID=756&TeacherID=163&ExamID=28ee67fb-2a0b-4744-8c23-81d915fd9690
    // &BeginTeachingTime=636298565880000000&ServerIP=http://127.0.0.1:87556

    //试题类型链接
    if (addr.indexOf('/ReTest/Index') >= 0) {
        var _param = GetRequest(addr)
        layer.alert(addr)
        _param['dt'] = new Date().getTime()
        vm.$router.replace({
            name: 'resource',
            params: _param //参数
        })
    } else if (addr.indexOf('Member/inClass') >= 0) {
        SetInClassData()
    } else {
        layer.alert(addr)
    }
}

function IsOnline(msgTime) {
    var time = new Date();
    // 程序计时的月从0开始取值后+1
    var m = time.getMonth() + 1;
    var t = time.getFullYear() + "-" + m + "-" +
        time.getDate() + " " + time.getHours() + ":" +
        time.getMinutes() + ":" + time.getSeconds();
    var diffSecone = getDiffSecone(msgTime, t);
    //差距在3秒内认为是在线消息
    if (diffSecone < 1) {
        return true;
    } else {
        return false;
    }
}

function getDiffSecone(date1, date2) {
    var reg = new RegExp("-", "g"); //创建正则RegExp对象
    var date1 = date1.replace(reg, "/");
    var date2 = date2.replace(reg, "/");
    var date1 = new Date(date1);
    var date2 = new Date(date2);
    var s1 = date1.getTime();
    var s2 = date2.getTime();
    return parseInt((s2 - s1) / 1000);
}
//openfire客户端回调函数
//openfire客户端会回调此函数，并返回响应内容，用户只能在此函数里追加自己的业务代码。
function HostData(ResponseType, ResponseContent) {
    if (!ResponseSet[ResponseType]) ResponseSet[ResponseType] = {}
    ResponseSet[ResponseType]['content'] = ResponseContent
    ResponseSet[ResponseType]['received'] = true

    //响应内容为在线用户信息
    if (ResponseType == "UserOnline") {
        var UserInfo = ResponseContent.split(','); //当前在线用户
    }
    //响应内容为返回投票信息
    else if (ResponseType == "AutoSend") {

    } else if (ResponseType == "LockScreen") {
        if (ResponseContent == "1") {
            var $ZeZao = $(".ZeZao");
            var BodyHeight = $(window).height();
            if ($ZeZao.length == 0) {
                var $ZeZao = $("<div class=\"ZeZao\">当前已锁定屏幕，请等候解锁...</div>").css({
                    "background-color": "#4390DF",
                    "width": "100%",
                    "position": "fixed",
                    "z-index": "999999",
                    "top": "0",
                    "left": "0",
                    "color": "#FFF",
                    "font-size": "30px",
                    "font-family": "微软雅黑",
                    "text-align": "center"
                });
                $("body").append($ZeZao);
                $(".ZeZao").css({
                    "height": BodyHeight + "px",
                    "line-height": BodyHeight + "px",
                    "margin-top": "-" + BodyHeight + "px"
                }).animate({
                    "margin-top": 0
                }, "slow");
                $("audio,video").each(function () {
                    this.pause();
                });
            }
        } else if (ResponseContent == "0") {
            $(".ZeZao").animate({
                "margin-top": "-" + $(window).height() + "px"
            }, "slow", null, function () {
                $(this).remove()
            })
        }
    } else {
        if (!!console)
            console.log(e)
        else
            layer.alert('ResponseType:' + ResponseType + ',ResponseContent:' + ResponseContent)
    }
}

//用户发起投票
//VoteID为用户指定一个唯一性的投票Id
//UserName参数表示在centor服务端接收到消息后转发给某个用户的账号,即投票结果发给哪个用户。
function UserVote(VoteID, UserName) {
    //教师端向发起Centor注册投票
    var register = "<CMD Name=\"QueryVote\" VoteID=\"" + VoteID + "\" Type=\"Total\"/>"; //
    window.open("http://cmd.com/SendMsg?centor&" + register) //向Centor账号发起.注册统计账号.

    //教师端命令Centor接收到投票信息转发给指定用户。
    var begin = "<CMD Name=\"AutoSend\" VoteID=\"" + VoteID + "\" AutoSendID=\"" + UserName + "\"/>"; //指定centor服务端接收到消息后转发给UserName用户。
    window.open("http://cmd.com/SendMsg?cento r&" + begin);
}

//学生投票
function StudentVote(VoteID, UserCode) {
    var _Param = "<CMD Name=\"Vote\" VoteID=\"" + VoteID + "\" StuID=\"" + UserCode + "\" StuName=\"" + UserCode + "\"/>";
    window.open("http://cmd.com/SendMsg?centor&" + _Param);
}

function ResponseHandler(Type, func) {
    try {
        if (!ResponseSet[Type]) ResponseSet[Type] = {}
        ResponseSet[Type]['callback'] = func
        ResponseSet[Type]['intval'] = setInterval(function () {
            try {
                if (!!ResponseSet[Type] && !!ResponseSet[Type].received) {
                    ResponseSet[Type].received = false
                    clearInterval(ResponseSet[Type]['intval'])
                    ResponseSet[Type]['callback'](ResponseSet[Type]['content'])
                }
            } catch (e) {
                layer.alert(e.message)
                if (!!console)
                    console.log(e)
            }
        }, 200)
    } catch (e) {
        layer.alert(e.message)
    }
}

function GetRequest(url) {
    url = url.substring(url.indexOf('?'))
    var theRequest = new Object()
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

function SetInClassData() {
    if (!window.mainRouteInfo && vm.$store.state.token != '') {
        vm.$http.get('api/lession/inClass?sk=' + vm.$store.state.token + '&cid=' + vm.$store.state.userInfo.ClassID).then(function (res) {
            if (res.data.state == 200) {
                setTimeout(function () {
                    if (!!res.data.data && !!res.data.data.Lession) {
                        window.mainRouteInfo = res.data.data
                        vm.$router.replace({
                            name: 'main'
                        })
                    } else {
                        vm.$router.push({
                            path: '/loading'
                        })
                    }
                }, 100);
            } else {
                vm.$router.push({
                    path: '/loading'
                })
            }
        })
    }
}